package com.kotle.baselib.retfofit;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by Administrator on 2017/7/3.
 */

class LogUtil {
    private final static String folderName="/kotleLog";
    private static SimpleDateFormat formate;

    public static String longTimeToString(long time) {
        SimpleDateFormat format = new SimpleDateFormat("YYYY年MM月DD HH：mm：SS");
        return format.format(new Date(time));
    }
    //将文件写入
    public static void writerToFile(String message) {
        try {
            /**
             * 创建日志文件名称
             */
            if (formate == null) {
                formate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            }
            String timer = formate.format(new Date());
            String fileName = timer+".log";
            /**
             * 创建文件夹
             */
            File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+folderName);
            if (!folder.exists())
                folder.mkdirs();
            /**
             * 创建日志文件
             */
            File file = new File(folder.getAbsolutePath()+File.separator+fileName);
            if (!file.exists())
                file.createNewFile();

            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(message);
            bufferedWriter.flush();
            bufferedWriter.close();

        }catch (Exception e) {
            Log.e("", "writerToFile - "+e.getMessage());
        }
    }
}
